﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace UnifiedTool.Model
{
    public static class MediaMetadata
    {
        private static List<MusicInfo> musicPlayItems = new List<MusicInfo>();

        private static int selectId = 0;


        public static int SelectId { get => selectId; set => selectId = value; }

        public static void addNewMusicItems(MusicInfo songInfo)
        {
            string message = "无法添加歌曲！歌曲ID重复。" ;
            Uri uri = null;
            
            if (File.Exists(songInfo.Uri))
            {
                string ext = Path.GetExtension(songInfo.Uri);
                if (ext == ".mp3" || ext == ".wav" || ext == ".wma")
                {
                    uri = new Uri(songInfo.Uri);
                    
                }
            }
            if (uri != null)
            {
                bool valid = false;
                musicPlayItems.ForEach(item => {
                    if (item.Id == songInfo.Id)
                    {
                        valid = true;
                        return;
                    }
                });
                if (!valid)
                {
                    musicPlayItems.Add(songInfo);
                    SelectId = songInfo.Id;
                    message = "歌曲添加成功！";
                }
                else {
                    SelectId = songInfo.Id;
                    message = "重新定位歌曲编号:" + SelectId;
                }
            }
            System.Diagnostics.Debug.WriteLine(message);

        }

        public static List<MusicInfo> getMusicItems() {
            return musicPlayItems;
        }

        public static MusicInfo getMusic(int id)
        {
            MusicInfo metadata = null;
               musicPlayItems.ForEach(item => {
                    if (item.Id == id) {
                       metadata = item;
                    }
                });

            return metadata;
        }



    }
}
